package 剑指offer;

import com.sun.java.swing.plaf.windows.WindowsTextAreaUI;
import leetcode.day_2020_12_14_12_21.TreeNode;

import javax.swing.*;
import java.util.*;

public class Offer32_JZ22 {
    // 从上到下打印二叉树
    public ArrayList<Integer> PrintFromTopToBottom1(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
        }
        return list;
    }

    // 分行从上到下打印二叉树
    public ArrayList<ArrayList<Integer>> PrintFromTopToBottom2(TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            while (size > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            result.add(list);
        }
        return result;
    }

    // 之字形打印队列
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return null;
        }
        Stack[] stacks = new Stack[2];
        stacks[0] = new Stack();
        stacks[1] = new Stack();
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        int current = 0;
        int next = 1;
        stacks[current].push(root);
        while (!stacks[0].isEmpty() || !stacks[1].isEmpty()) {
            TreeNode node = (TreeNode) stacks[current].pop();
            list.add(node.val);
            if (current == 0) {
                if (root.left != null) {
                    stacks[next].push(root.left);
                }
                if (root.right != null) {
                    stacks[next].push(root.right);
                }
            } else {
                if (root.right != null) {
                    stacks[next].push(root.right);
                }
                if (root.left != null) {
                    stacks[next].push(root.left);
                }
            }
            if (stacks[current].isEmpty()) {
                result.add(new ArrayList<>(list));
                current = 1-current;
                next = 1-next;
            }
        }
        return result;
    }

    public List<List<Integer>> levelOrder2(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> result = new ArrayList<>();
        if (root==null) {
            return result;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            LinkedList<Integer> list = new LinkedList<>();
            int size = queue.size();
            while (size>0) {
                TreeNode node = queue.poll();
                if (result.size() % 2 == 0) {
                    list.addLast(node.val);
                } else {
                    list.addFirst(node.val);
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            result.add(list);
        }
        return result;
    }
}
